<?php

class Daftar_con extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('date_func');
    }

    function index() {
    //menampilkan halaman login
        if($this->session->userdata('Loggedin')==true){
            $this->home();
        }else{
            $this->form_login();
        }
    }

    function form_login() {
        
        //melakukan query untuk no term dan tahun, dan menyimpannya di session
        $term_thn = $this->db->get_where('term',array('STATUS'=>'SEDANG'))->row_array();
        $data_session = array(
            'term'=>$term_thn['NO_TERM'],
            'idterm'=>$term_thn['ID_TERM'],
            'tahun'=>$term_thn['TAHUN'],
            'status'=>$term_thn['STATUS']
        );
        
        $this->session->set_userdata($data_session);
        
        $display['noterm'] = $this->session->userdata('term');
        $display['thnterm'] = $this->session->userdata('tahun');
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/login_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function validate_login() {
        
        //Melakukan query untuk mengambil data user yang login
        $usr = $this->input->post('username');
        $password = $this->input->post('password');
        $strquery = "SELECT *
                FROM (
                user
                NATURAL JOIN role
                )
                NATURAL JOIN daftar_role
                WHERE USERNAME='" . $usr . "'
                AND PASSWORD='" . md5($password) . "'
                AND NAMA_ROLE='FO'    
                ";
        $query = $this->db->query($strquery);
        //jika gagal login
        if ($query->num_rows() == 0) {
            redirect(base_url().'pendaftaran');
        }
        //jika sukses login
        $result = $query->row_array();
        
        //Melakukan query jumlah pendaftar yang mendaftar pada si user
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=" .$result['ID']. " AND ID_TERM=" . $this->session->userdata('idterm');
        
        $jumlah = $this->db->query($strquery)->row_array();
        
        $data = array(
            'Loggedin' => TRUE,
            'Nama' => $result['NAMA'],
            'ID' => $result['ID'],
            'jumlah'=>$jumlah['jumlah']
        );
        $this->session->set_userdata($data);
    
        redirect(base_url().'pendaftaran/daftar_con/home');
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'pendaftaran');
    }

    function home() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        //query untuk mendapatkan total jumlah pendaftar
        $idterm=$this->session->userdata('idterm');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm;
        $display['total'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan total jumlah pendaftar yang gratis
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm." and TOTAL_BAYAR=0";
        $display['total_gratis'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan pendaftar hari ini
        $today = date('Y-m-d');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm." and TGL_DAFTAR='".$today."'";
        $display['total_today'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan pendaftar kemarin
        $yesterday = date_create($today);
        date_sub($yesterday, date_interval_create_from_date_string('1 days'));
        $yesterday = date_format($yesterday, 'Y-m-d');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm." and TGL_DAFTAR='".$yesterday."'";
        $display['total_yesterday'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan pendaftar minggu ini
        $num_day = date('w');
        $this_week = date_create($today);
        date_sub($this_week, date_interval_create_from_date_string($num_day.' days'));
        $this_week = date_format($this_week, 'Y-m-d');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm." and TGL_DAFTAR>='".$this_week."'";
        $display['total_thisweek'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan pendaftar minggu lalu
        $last_week = date_create($this_week);
        date_sub($last_week, date_interval_create_from_date_string('7 days'));
        $last_week = date_format($last_week, 'Y-m-d');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm." and TGL_DAFTAR>='".$last_week."' and TGL_DAFTAR<'".$this_week."'";
        $display['total_lastweek'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan pelatihan pada term yang aktif
        $strquery = 'select * from pelatihan natural join kelas where TERM='.$this->session->userdata('term');
        $display['hasil'] = $this->db->query($strquery)->result_array();
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/home_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function list_pendaftar($kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        $hasil = $this->db->query('select * from pelatihan natural join kelas where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->row_array();
        $display['namapelatihan']=$hasil['NAMA_PELATIHAN'];
        $display['kode']=$kode;
        $display['angkatan']=$angkatan;
        $display['kuota']=$hasil['KUOTA'];
        $display['harga']=$hasil['HARGA'];

        $strqueryhari = "SELECT DISTINCT HARI FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $display['kode'] . "' AND ANGKATAN=" . $display['angkatan'] . " ORDER BY HARI";
        $reshari = $this->db->query($strqueryhari)->result_array();

        for ($i = 0; $i < count($reshari); $i++) {
            $strquerywaktumin = "SELECT MIN(KODE_WAKTU) as Min FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $display['kode'] . "' AND ANGKATAN =" . $display['angkatan'] . " AND HARI=" . $reshari[$i]['HARI'];
            $waktumin = $this->db->query($strquerywaktumin)->row()->Min;
            $strquerywaktumax = "SELECT MAX(KODE_WAKTU) as Max FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $display['kode'] . "' AND ANGKATAN =" . $display['angkatan'] . " AND HARI=" . $reshari[$i]['HARI'];
            $waktumax = $this->db->query($strquerywaktumax)->row()->Max;
            $display['waktu'][$i]['hari'] = $this->konversi_hari($reshari[$i]['HARI']);
            $display['waktu'][$i]['jam'] = $this->konversi_kode_waktu($waktumin, $waktumax);
        }

        $jum = $this->db->query('select count(ID_USER) as jum from peserta_kelas where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->row();
        $display['jumlah'] = $jum->jum;

        $display['pendaftar'] = $this->db->query('select * from user inner join peserta_kelas on user.ID=peserta_kelas.ID_USER where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->result_array();
        
        //Query tanggal untuk sesi pertama
        $query_sesi_pertama = 'select * from daftar_jadwal where KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan.' and NOMOR_SESI=1';
        $tgl  =$this->db->query($query_sesi_pertama)->result_array();
        if(count($tgl)>0){
            $tgl_sesi = date_create($tgl[0]['TANGGAL']);
            $display['tgl_sesi'] = date_format($tgl_sesi, 'd F Y');
        }else{
            $display['tgl_sesi']='TBN';
        }
        
        //Query untuk uang yang masuk
        $strquery = 'select distinct sum(JUMLAH) as masuk from pembayaran where KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan;
        $display['pemasukan'] = $this->db->query($strquery)->row()->masuk;       
        
        //Query untuk uang yang harusnya masuk
        $strquery = 'select distinct sum(TOTAL_BAYAR) as total from peserta_kelas where KODE_PELATIHAN="'.$kode.'" and ANGKATAN='.$angkatan;
                
        $display['total_harus_bayar'] = $this->db->query($strquery)->row()->total;
        $display['piutang']=$display['total_harus_bayar']-$display['pemasukan'];
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/pendaftar_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/pilih_daftar_view');
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_new() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $query = $this->db->get_where('kelas',array('STATUS'=>'BELUM'));
        $display['pelatihan'][''] = '';
        foreach ($query->result() as $entry) {
            $kode = $entry->KODE_PELATIHAN . '-' . $entry->ANGKATAN;
            //echo $kode;
            $display['pelatihan'][$kode] = $kode;
        }
        
        $query = $this->db->get('diskon');
        $i = 0;
        foreach ($query->result() as $entry) {
            $display['diskon'][$i]['no'] = $entry->ID_DISKON;
            $display['diskon'][$i]['nama'] = $entry->NAMA_DISKON;
            $i++;
        }
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas',$display_atas);
        $this->load->view('pendaftaran/form_new_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_new_proses() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $nama = $this->input->post('nama');
        $nama = rtrim($nama); //menghapus spasi pada akhir kata
        $nohp = $this->input->post('nohp');
        $gol = $this->input->post('gol');
        $kelas = $this->input->post('kelas');
        $username = str_ireplace(' ', '.', strtolower($nama));
        $password = md5($username);
        
        //mengecek apakah username sudah ada
        $isvalid=$this->db->get_where('user',array('USERNAME'=>$username))->result_array();
        if(count($isvalid)>0){
            //username fail
            $data['pesan'] = 'Username sudah ada <br/> <a href="#" onclick="back()">Kembali</a>';
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display_atas);
            $this->load->view('pendaftaran/pesan_kesalahan', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }else{
            $data = array(
                'NAMA' => $nama,
                'NO_TELP' => $nohp,
                'USERNAME' => $username,
                'PASSWORD' => $password,
				'GOL' => $gol
            );

            $this->db->insert('user', $data);
            $strquery = "select ID from user where USERNAME='" . $username . "'";
            $id = $this->db->query($strquery)->row()->ID;
            $id_role = $this->db->get_where('daftar_role', array('NAMA_ROLE' => 'Peserta'))->row()->ID_ROLE;
            // insert role sebagai peserta
            $inrole = array('ID' => $id, 'ID_ROLE' => $id_role);
			$this->db->insert('role', $inrole);
            // inisiasi diskon awal = 0%
            $besar_diskon = 0;
            $no = $this->input->post('nocic');
            $num_rows = '';
            // mengecek diskon dari kartu CIC milik orang
            if ($no != '') {   
                $query = $this->db->get_where('peserta', array(
                            'NO_CIC_CARD' => $no, 
							'STATUS_CIC_CARD' => 1
                        ));
                $num_rows = $query->num_rows();
                if ($query->num_rows() == 1) {
                    $besar_diskon = 20;
                    $update = array('STATUS_CIC_CARD' => 0);
                    $this->db->where('NO_CIC_CARD', $no);
                    $this->db->update('peserta', $update);
                    
                    //Insert ke database dapetin diskon CICcard punya orang
                    $pelatihan = explode('-', $kelas);
                    $kode = $pelatihan[0];
                    $angkatan = $pelatihan[1];
                    $data_diskon = array(
                        'ID_USER'=>$id,
                        'NAMA_DISKON'=>'Diskon CICCard orang',
                        'BESAR_DISKON'=>$besar_diskon,
                        'ANGKATAN'=>$angkatan,
                        'KODE_PELATIHAN'=>$kode
                    );
                    $this->db->insert('get_diskon',$data_diskon);
                }else{
                    //diskon fail
                    $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="#" onclick="back()">Kembali</a>';
                    $this->load->view('pendaftaran/daftar_header_view');
                    $this->load->view('pendaftaran/menu_atas',$display_atas);
                    $this->load->view('pendaftaran/pesan_kesalahan', $data);
                    $this->load->view('pendaftaran/daftar_footer_view');
                }
            } else {
                //ngecek diskon yg dicentang
                $diskon = $this->input->post('diskon');
                if (!empty($diskon)) {
                    for ($i = 0; $i < count($diskon); $i++) {
                        $hasil = $this->db->get_where('diskon', array('ID_DISKON' => $diskon[$i]))->row();
                        if (strcasecmp($hasil->NAMA_DISKON, 'diskon golongan') == 0) {
                            // Mendapat diskon golongan
                            switch ($gol) {
                                case 1:
                                    $get_diskon = 10;
                                    $besar_diskon+=10;
                                    break;
                                case 2:
                                    $get_diskon = 10;
                                    $besar_diskon+=10;
                                    break;
                                case 3:
                                    $get_diskon = 5;
                                    $besar_diskon+=5;
                                    break;
                                case 4:
                                    $get_diskon = 0;
                                    $besar_diskon+=0;
                                    break;
                                case 5:
                                    $get_diskon = 100;
                                    $besar_diskon+=100;
                                    break;
                                case 6:
                                    $get_diskon = 100;
                                    $besar_diskon+=100;
                                    break;
                            }
                        } else {
                            // Mendapat diskon yang didefinisiin di database
                            $besar_diskon+=$hasil->BESAR;
                            $get_diskon = $hasil->BESAR;
                        }
                        
                        // Insert data diskon ke database
                        $pelatihan = explode('-', $kelas);
                        $kode = $pelatihan[0];
                        $angkatan = $pelatihan[1];
                        $data_diskon = array(
                            'ID_USER'=>$id,
                            'NAMA_DISKON'=>$hasil->NAMA_DISKON,
                            'BESAR_DISKON'=>$get_diskon,
                            'ANGKATAN'=>$angkatan,
                            'KODE_PELATIHAN'=>$kode
                        );
                        $this->db->insert('get_diskon',$data_diskon);
                        
                    }
                }
            }
            if ($num_rows == 1 || $no == '') {
                $pelatihan = explode('-', $kelas);
                $kode = $pelatihan[0];
                $angkatan = $pelatihan[1];
                $result = $this->db->get_where('kelas', array(
                            'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row();
                $harga = $result->HARGA;
                if ($besar_diskon > 100)
                    $besar_diskon = 100;

                $temp = (100 - $besar_diskon) / 100 * $harga;
                $total_bayar = $this->division2($temp, 1000);
                $data = array(
                    'ID_USER' => $id,
                    'ANGKATAN' => $angkatan,
                    'KODE_PELATIHAN' => $kode,
                    'TOTAL_BAYAR' => $total_bayar,
                    'TGL_DAFTAR' => date('Y-m-d'),
                    'ID_TERM' => $this->session->userdata('idterm'),
                    'ID_FO' => $this->session->userdata('ID')
					
                );
                $this->db->insert('peserta_kelas', $data);
				
				// menggenerate no cic card
				// mengecek apakah ada nomor yg ksosong di database
				// $cic_card_kosong = $this->db->get('no_cic_card_kosong')->result_array();
				// if(count($cic_card_kosong)>0){
				//	$gen_nocic = $cic_card_kosong[0]['NO_CIC_CARD'];
				//	$this->db->delete('no_cic_card_kosong',array('id'=>$cic_card_kosong[0]['ID']));
				//}else{
					$nourut = $this->cari_urut();
					$gen_nocic = $this->convert_no($nourut) . '-' . $this->cut_year($this->session->userdata('tahun')) . $gol;
				//}
				
				$data = array('NO_CIC_CARD' => $gen_nocic, 'ID' => $id, 'STATUS_CIC_CARD' => 'FALSE');
                $this->db->insert('peserta ', $data);
                $data['id'] = $id;
                $data['nama'] = $nama;
                $data['nohp'] = $nohp;
                $data['no_cic'] = $gen_nocic;
                $data['kelas'] = $kelas;
                $data['pelatihan'] = $kode;
                $data['angkatan'] = $angkatan;
                $data['biaya'] = $harga;
                $data['diskon'] = $besar_diskon;
                $data['bayar'] = $total_bayar;


                $display_atas['nama']=$this->session->userdata('Nama');
                $display_atas['jumlah']=$this->session->userdata('jumlah');
                $display_atas['noterm']=$this->session->userdata('term');
                $display_atas['thnterm']=$this->session->userdata('tahun');
                // query list term
                $query_list_term = $this->db->get('term')->result_array();
                for($i=0;$i<count($query_list_term);$i++){
                    $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
                }
                $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');

                
                $result = $this->db->query('select count(JUMLAH) as total from pembayaran where ID=' . $id)->row();
                $data['sudah_bayar'] = $result->total;
                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas', $display_atas);
                $this->load->view('pendaftaran/biaya_view', $data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }
        }
    }

    function cari_urut() {
        $query = $this->db->query('SELECT MAX(NO_CIC_CARD) as find FROM peserta');
        $result = $query->row_array();
		$str_arr = explode('-', $result['find']);
		
        return (intval($str_arr[0]) + 1);
    }

	
    function display_biaya($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];
        
        

        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];
        $data['pelatihan'] = $pelatihan;
        $data['angkatan'] = $angkatan;
        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

		//query jumlah diskon yang didapatkan
		$all_disc = $this->db->query('select sum(BESAR_DISKON) as total_diskon from get_diskon where ID_USER=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
		
		$data['diskon']=$all_disc->total_diskon;
		if($data['diskon']==NULL)
			$data['diskon']=0;
		
		
		//jika hasil query sama dengan 0, hitung jumlah diskon dengan cara manual
		if($data['diskon']==0){
			$potongan = $data['biaya'] - $data['bayar'];
			$data['diskon'] = round($potongan * 100 / $data['biaya']);
		}
		
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/biaya_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function bayar($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        
        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];

        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

        $potongan = $data['biaya'] - $data['bayar'];

        $data['diskon'] = round($potongan * 100 / $data['biaya']);

        $this->db->order_by('TGL_BAYAR', 'asc');
        $data['history'] = $this->db->get_where('pembayaran', array('ID' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->result_array();
        
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/bayar_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function input_bayar($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

		$display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
		
		
        $besar_bayar = $this->input->post('besar');
		
		if($besar_bayar==0){
			$data['pesan'] = 'Pembayaran tidak boleh 0 <br/> <a href="#" onclick="back()">Kembali</a>';
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas',$display_atas);
            $this->load->view('pendaftaran/pesan_kesalahan', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
		}else{
		
			$tgl = date('Y-m-d H:i:s');

			$data = array(
				'ID' => $id,
				'TGL_BAYAR' => $tgl,
				'JUMLAH' => $besar_bayar,
				'ID_FO' => $this->session->userdata('ID'),
				'KODE_PELATIHAN' => $kode,
				'ANGKATAN' => $angkatan
			);
			$this->db->insert('pembayaran', $data);

			$user = $this->db->get_where('user', array('ID' => $id))->row_array();
			$data['id'] = $id;
			$data['nama'] = $user['NAMA'];
			$data['nohp'] = $user['NO_TELP'];
			$peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
			$data['no_cic'] = $peserta['NO_CIC_CARD'];

			$data['tgl'] = $tgl;
			$data['besar'] = $besar_bayar;

			$peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

			$pelatihan = $peserta_kelas['KODE_PELATIHAN'];
			$angkatan = $peserta_kelas['ANGKATAN'];
			$data['kode'] = $pelatihan;
			$data['angkatan'] = $angkatan;
			$data['kelas'] = $pelatihan . '-' . $angkatan;



			$this->load->view('pendaftaran/daftar_header_view');
			$this->load->view('pendaftaran/menu_atas', $display_atas);
			$this->load->view('pendaftaran/struk_view', $data);
			$this->load->view('pendaftaran/daftar_footer_view');
		}
    }

    function convert_no($no) {
        $strno = '' . $no;
        for ($i = 0; $i < 5 - strlen($strno); $i++) {
            $strno = '0' . $strno;
        }
        return $strno;
    }

    function cut_year($year) {
        $stryear = '' . $year;
        return substr($stryear, 2);
    }

    function daftar_old() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/cari_nocic');
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function cari_user() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $nocic = $this->input->post('nocic');
        $strquery = 'select * from peserta inner join user on peserta.ID=user.ID where peserta.NO_CIC_CARD="' . $nocic . '"';
        $query = $this->db->query($strquery);
        //echo $nocic.' '.$strquery;
        if ($query->num_rows == 1) {
            $hasil = $query->row_array();
            $data['id'] = $hasil['ID'];
            $data['nama'] = $hasil['NAMA'];
            $data['nocic'] = $hasil['NO_CIC_CARD'];
            $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan <br/> <a href="' . base_url() . 'pendaftaran/daftar_con/daftar">Daftar baru</a>';
            $display_atas['nama']=$this->session->userdata('Nama');
            $display_atas['jumlah']=$this->session->userdata('jumlah');
            $display_atas['noterm']=$this->session->userdata('term');
            $display_atas['thnterm']=$this->session->userdata('tahun');
            // query list term
            $query_list_term = $this->db->get('term')->result_array();
            for($i=0;$i<count($query_list_term);$i++){
                $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
            }
            $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');

            
            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas', $display_atas);
            $this->load->view('pendaftaran/hasilcari_view', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        } else {
            $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan <br/> <a href="' . base_url() . 'pendaftaran/daftar_con/daftar">Daftar baru</a>';
            $display_atas['nama']=$this->session->userdata('Nama');
            $display_atas['jumlah']=$this->session->userdata('jumlah');
            $display_atas['noterm']=$this->session->userdata('term');
            $display_atas['thnterm']=$this->session->userdata('tahun');

            // query list term
            $query_list_term = $this->db->get('term')->result_array();
            for($i=0;$i<count($query_list_term);$i++){
                $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
            }
            $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');

            
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas', $display_atas);
            $this->load->view('pendaftaran/pesan_kesalahan', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
    }

    function form_daftar_old($id) {

        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $query = $this->db->get_where('kelas',array('STATUS'=>'BELUM'));
        $data['pelatihan'][''] = '';
        foreach ($query->result() as $entry) {
            $kode = $entry->KODE_PELATIHAN . '-' . $entry->ANGKATAN;
            $data['pelatihan'][$kode] = $kode;
        }
        $query = $this->db->get('diskon');
        $i = 0;
        foreach ($query->result() as $entry) {
            $data['diskon'][$i]['no'] = $entry->ID_DISKON;
            $data['diskon'][$i]['nama'] = $entry->NAMA_DISKON;
            $i++;
        }

        $data['user'] = $this->db->query('select * from user where ID=' . $id)->row_array();
        $data['peserta'] = $this->db->query('select * from peserta where ID=' . $id)->row_array();

        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/form_old_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_old_proses() {

        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        $id = $this->input->post('id');
        $nama = $this->input->post('nama');
        $nohp = $this->input->post('nohp');
        $gol = $this->input->post('gol');
        $kelas = $this->input->post('kelas');

        $data = array(
                'NAMA' => $nama,
                'NO_TELP' => $nohp,
				'GOL' => $gol
            );
        $this->db->where('id',$id);
        $this->db->update('user', $data);
        
        $besar_diskon = 0;

        $no = $this->input->post('nocic');

                
        if ($no != '') {
            $query = $this->db->get_where('peserta', array(
                        'NO_CIC_CARD' => $no, 'STATUS_CIC_CARD' => 1
                    ));
            if ($query->num_rows() == 1) {
                $besar_diskon = 25;
                $update = array('STATUS_CIC_CARD' => 0);
                $this->db->where('NO_CIC_CARD', $no);
                $this->db->update('peserta', $update);
                //Insert diskon CICCard ke database
                $pelatihan = explode('-', $kelas);
                $kode = $pelatihan[0];
                $angkatan = $pelatihan[1];
                $id = $query->row()->ID;
                $data_diskon = array(
                    'ID_USER'=>$id,
                    'NAMA_DISKON'=>'Diskon CICCard sendiri',
                    'BESAR_DISKON'=>$besar_diskon,
                    'ANGKATAN'=>$angkatan,
                    'KODE_PELATIHAN'=>$kode
                );
                $this->db->insert('get_diskon',$data_diskon);
                
            } else {
                
                //diskon fail
                redirect(base_url() . 'pendaftaran/daftar_con/ciccardfail');
            }
        } else {
            //ngecek diskon yg dicentang
            $diskon = $this->input->post('diskon');
            if (!empty($diskon)) {
                for ($i = 0; $i < count($diskon); $i++) {
                    $hasil = $this->db->get_where('diskon', array('ID_DISKON' => $diskon[$i]))->row();
                    if (strcasecmp($hasil->NAMA_DISKON, 'diskon golongan') == 0) {
                        switch ($gol) {
                            case 1:
                                $get_diskon=10;
                                $besar_diskon+=10;
                                break;
                            case 2:
                                $get_diskon=10;
                                $besar_diskon+=10;
                                break;
                            case 3:
                                $get_diskon=5;
                                $besar_diskon+=5;
                                break;
                            case 4:
                                $get_diskon=0;
                                $besar_diskon+=0;
                                break;
                            case 5:
                                $get_diskon=100;
                                $besar_diskon+=100;
                                break;
                            case 6:
                                $get_diskon=100;
                                $besar_diskon+=100;
                                break;
                        }
                    } else {
                        $get_diskon=$hasil->BESAR;
                        $besar_diskon+=$hasil->BESAR;
                    }
                    // Insert data diskon ke database
                    $pelatihan = explode('-', $kelas);
                    $kode = $pelatihan[0];
                    $angkatan = $pelatihan[1];
                    $data_diskon = array(
                        'ID_USER'=>$id,
                        'ID_DISKON'=>$diskon[$i],
                        'NAMA_DISKON'=>$hasil->NAMA_DISKON,
                        'BESAR_DISKON'=>$get_diskon,
                        'ANGKATAN'=>$angkatan,
                        'KODE_PELATIHAN'=>$kode
                    );
                    $this->db->insert('get_diskon',$data_diskon);
                }
                
            }
        }

        $pelatihan = explode('-', $kelas);
        $kode = $pelatihan[0];
        $angkatan = $pelatihan[1];
        $result = $this->db->get_where('kelas', array(
                    'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row();
        $harga = $result->HARGA;
        $tb = (100 - $besar_diskon) / 100 * $harga;
	$total_bayar = $this->division2($tb,1000);
        $id = $this->input->post('id');
        $data = array(
            'ID_USER' => $id,
            'ANGKATAN' => $angkatan,
            'KODE_PELATIHAN' => $kode,
            'TOTAL_BAYAR' => $total_bayar,
            'TGL_DAFTAR' => date('Y-m-d'),
            'ID_TERM' => $this->session->userdata('idterm'),
            'ID_FO' => $this->session->userdata('ID'),
			
        );
        $this->db->insert('peserta_kelas', $data);
        redirect(base_url() . 'pendaftaran/daftar_con/display_biaya/' . $id . '/' . $kode . '/' . $angkatan);
    }

    function ciccardfail(){
        $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="#" onclick="back()">Kembali</a>';
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/pesan_kesalahan', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }
    
    function history($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

		$display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
		
		
        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];

        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

        $potongan = $data['biaya'] - $data['bayar'];

        $data['diskon'] = $potongan * 100 / $data['biaya'];

        $this->db->order_by('TGL_BAYAR', 'asc');
        $data['history'] = $this->db->get_where('pembayaran', array('ID' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->result_array();

        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/history_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function konversi_hari($nomor) {
        switch ($nomor) {
            case 1: return 'Senin';
            case 2: return 'Selasa';
            case 3: return 'Rabu';
            case 4: return 'Kamis';
            case 5: return 'Jumat';
            case 6: return 'Sabtu';
            case 7: return 'Minggu';
        }
    }

    function division($bil1, $bil2) {
        $temp = ($bil1 - 1) / $bil2;
        $temp2 = ($bil1 - 1) % $bil2;
        return ($temp - ($temp2 / $bil2));
    }

    function division2($bil1='', $bil2='') {

        $hasil = $bil1 / $bil2;
        $hasil = round($hasil);
        return 1000 * $hasil;
    }

    function isgenap($nomor) {
        return (($nomor % 2) == 0);
    }

    function konversi_kode_waktu($nomor1, $nomor2) {
        $hasil = 7 + $this->division($nomor1, 2);
        $hasil .= '.';
        if (!$this->isgenap($nomor1))
            $hasil .= '00';
        else
            $hasil .= '30';
        $hasil .= '-';

        if ($this->isgenap($nomor2)) {
            $hasil .= 7 + $this->division($nomor2, 2) + 1;
            $hasil .= '.';
            $hasil .= '00';
        } else {
            $hasil .= 7 + $this->division($nomor2, 2);
            $hasil .= '.';
            $hasil .= '30';
        }
        return $hasil;
    }

//	function batal_bayar($id, $kode, $angkatan, $waktu) {
//		$data = array(
//			'ID'=>$id,
//			'KODE_PELATIHAN'=>$kode,
//			'ANGKATAN'=>$angkatan,
//			'TGL_BAYAR'=>str_replace('%20',' ', $waktu)
//		);
////		var_dump($data);
//		$this->db->delete('pembayaran', $data);
////		die();
//		redirect(base_url().'pendaftaran/daftar_con/bayar/'.$id.'/'.$kode.'/'.$angkatan);
//	}
	
	
    function cetak_kuitansi($id, $kode, $angkatan, $waktu) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $result = $this->db->get_where('user', array('ID' => $id));
        $array = $result->result_array();
        $data['nama_peserta'] = $array[0]['NAMA'];
        $data['tanggal'] = date('d m Y H.i');
        $data['nama_petugas'] = $this->session->userdata('Nama');
        $data['no_hp_peserta'] = $array[0]['NO_TELP'];
        $result = $this->db->get_where('peserta', array('ID' => $id));
        $array = $result->result_array();
        $data['no_cic_peserta'] = $array[0]['NO_CIC_CARD'];
        $result = $this->db->get_where('pelatihan', array('KODE_PELATIHAN' => $kode));
        $array = $result->result_array();
        $data['nama_pelatihan'] = $array[0]['NAMA_PELATIHAN'];
        $result = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan));
        $array = $result->result_array();
        $data['jam_total'] = $array[0]['JAM_TOTAL'];
        $data['pertemuan'] = $array[0]['JAM_TOTAL'] / $array[0]['JAM_PER_SESI'];
        $data['term'] = $array[0]['TERM'];
        $data['harga'] = $array[0]['HARGA'];
        $data['harga2'] = $data['harga'];
        $data['sesi_per_minggu'] = $array[0]['SESI_PER_MINGGU'];
        $result = $this->db->get_where('term', array('NO_TERM' => $data['term']));
        $array = $result->result_array();
        $data['tahun_term'] = $array[0]['TAHUN'];
        $result = $this->db->query('SELECT ID_RUANGAN, HARI
                                        FROM  jadwal_pelatihan
                                        WHERE KODE_PELATIHAN="' . $kode . '"
                                        AND ANGKATAN = ' . $angkatan . '
                                        GROUP BY  HARI
                                        LIMIT 0 , 30');
        $array = $result->result_array();
        $hari = array();
        $jam = array();
        for ($i = 0; $i < count($array); $i++) {
            $hari[$i] = $this->konversi_hari($array[$i]['HARI']);
            $result = $this->db->query('SELECT MIN(KODE_WAKTU) AS min,MAX(KODE_WAKTU) as max FROM `jadwal_pelatihan` WHERE HARI =' . $array[$i]['HARI'] . ' AND KODE_PELATIHAN="' . $kode . '"
                                        AND ANGKATAN = ' . $angkatan);
            $array2 = $result->result_array();
            $jam[$i] = $this->konversi_kode_waktu($array2[0]['min'], $array2[0]['max']);
        }
        $data['hari'] = $hari;
        $data['jam'] = $jam;

        $result = $this->db->query('SELECT *
                                        FROM  pembayaran WHERE ID=' . $id . ' AND
                                        KODE_PELATIHAN = "' . $kode . '" AND
                                        ANGKATAN = ' . $angkatan . '
                                        ORDER BY  TGL_BAYAR ASC ');
        $array = $result->result_array();
        
        /** Pengambilan data dari hasi query*/
        $idx=0;
        $waktu = str_replace('%20',' ', $waktu);
        for($i=0;$i<count($array);$i++){
            if($array[$i]['TGL_BAYAR']==$waktu){
               $idx=$i;
               break;
            }
        }
        
        $data['jumlah_pembayaran'] = $array[$idx]['JUMLAH'];
        $data['tanggal_pembayaran'] = $this->date_func->date_konversion_format2($array[$idx]['TGL_BAYAR']);
        
        $result = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'ANGKATAN' => $angkatan, 'KODE_PELATIHAN' => $kode));
        $array = $result->result_array();
        $data['total_bayar'] = $array[0]['TOTAL_BAYAR'];
        $result = $this->db->query('SELECT SUM(JUMLAH) AS jumlah
                                        FROM  pembayaran WHERE ID=' . $id . ' AND
                                        KODE_PELATIHAN = "' . $kode . '" AND
                                        ANGKATAN = ' . $angkatan . '
                                        ORDER BY  TGL_BAYAR DESC ');
        $array = $result->row_array();
        
        $data['pembayaran_ke'] = $idx+1;
        $data['sisa_pembayaran'] = $data['total_bayar'] - $array['jumlah'];
        $data['diskon'] = round(($data['harga'] - $data['total_bayar']) / $data['harga'] * 100);
        
        
        
        $this->load->view('pendaftaran/print', $data);
    }
    
    function edit_data($id,$kode,$angkatan,$err_msg=''){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        
        $data_user = $this->db->get_where('user',array('ID'=>$id))->row_array();
        //kode kesalahan yang ada :
        //1. username sudah digunakan
        //2. ada ketidakcocokan password baru dan password konfirmasi
        if($err_msg==''){
            $display['errmsg']='';
        }else{
            if($err_msg==1){
                $display['errmsg']='Username tersebut sudah digunakan, coba username yang lain';
            }else if($err_msg==2){
                $display['errmsg']='Konfirmasi password tidak cocok';
            }else{
                $display['errmsg']='';
            }
        }
        $display['kode']=$kode;
        $display['angkatan']=$angkatan;
        $display['nama']=$data_user['NAMA'];
        $display['notelp']=$data_user['NO_TELP'];
        $display['id']=$id;
        $display['password']=$data_user['PASSWORD'];
        $display['username']=$data_user['USERNAME'];
        
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/edit_data', $display);
        $this->load->view('pendaftaran/daftar_footer_view');   
    }
    
    function edit_data_process(){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        
        $id = $this->input->post('id');
        $data_user = $this->db->get_where('user',array('ID'=>$id))->row_array();
        
        $nama = $this->input->post('nama');
        $username = $this->input->post('username');
        $notelp = $this->input->post('notelp');
        $pass1 = $this->input->post('pass1');
        $pass2 = $this->input->post('pass2');
        $kode = $this->input->post('kode');
        $angkatan = $this->input->post('angkatan');
        
        $data_update=array();
        
        $data_update['NAMA']=$nama;
        $data_update['NO_TELP']=$notelp;
        
        if($data_user['USERNAME']!=$username){ //ada perubahan username, cek dulu
            $cek_user = $this->db->get_where('user',array('USERNAME'=>$username))->row_array();
            if(count($cek_user)!=0){ //sudah dipakai
                redirect(base_url().'pendaftaran/daftar_con/edit_data/'.$id.'/'.$kode.'/'.$angkatan.'/1');
            }else{
                $data_update['USERNAME']=$username;
            }
        }else{ //tidak ada perubahan username
            $data_update['USERNAME']=$username;
        }
        
        if($pass1!=''||$pass2!=''){ //kalau password diisi
            if($pass1!=$pass2){ //error karena password tidak sama
                redirect(base_url().'pendaftaran/daftar_con/edit_data/'.$id.'/'.$kode.'/'.$angkatan.'/2');
            }else{
                $data_update['PASSWORD']=md5($pass1);
            }
        }
        
        $this->db->where('ID',$id);
        $this->db->update('user',$data_update);
        redirect(base_url().'pendaftaran/daftar_con/display_biaya/'.$id.'/'.$kode.'/'.$angkatan);
    }
    
    function administrasi($id,$kode,$angkatan){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        //query untuk data peserta
        $strquery='select * from user natural join peserta where ID='.$id;
        $data_peserta = $this->db->query($strquery)->row_array();
        $display['id']=$id;
        $display['nama']=$data_peserta['NAMA'];
        $display['notelp']=$data_peserta['NO_TELP'];
        $display['nocic']=$data_peserta['NO_CIC_CARD'];
        $display['ciccard']=$data_peserta['STATUS_AMBIL_KARTU'];
        $display['kode']=$kode;
        $display['angkatan']=$angkatan;
        
        //query untuk kelas yang diikuti
        $strquery='select * from peserta_kelas where ID_USER='.$id.' and ANGKATAN='.$angkatan.' and KODE_PELATIHAN="'.$kode.'"';
        $data_kelas = $this->db->query($strquery)->row_array();
        $display['kit']=$data_kelas['DAPAT_KIT'];
        $display['sertifikat']=$data_kelas['DAPAT_SERTIFIKAT'];
        
        $display_atas['nama']=$this->session->userdata('Nama');
        $display_atas['jumlah']=$this->session->userdata('jumlah');
        $display_atas['noterm']=$this->session->userdata('term');
        $display_atas['thnterm']=$this->session->userdata('tahun');
        // query list term
        $query_list_term = $this->db->get('term')->result_array();
        for($i=0;$i<count($query_list_term);$i++){
            $display_atas['list_term']['Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN']]='Term '.$query_list_term[$i]['NO_TERM'].' '.$query_list_term[$i]['TAHUN'];
        }
        $display_atas['selected_term']='Term '.$this->session->userdata('term').' '.$this->session->userdata('tahun');
        
        
        
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $display_atas);
        $this->load->view('pendaftaran/administrasi_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }
    
    function admin_process($id,$kode,$angkatan){        
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
        //mengecek apakah kit dicentang
        if(isset($_POST['kit'])){
            $status_kit=1;
        }else{
            $status_kit=0;
        }
        //mengecek apakah sertifikat dicentang
        if(isset($_POST['sertifikat'])){
            $status_sertifikat=1;
        }else{
            $status_sertifikat=0;
        }
        //mengecek apakah kartu cic dicentang
        if(isset($_POST['ciccard'])){
            $status_ciccard=1;
        }else{
            $status_ciccard=0;
        }
        $kondisi = 'where ID_USER='.$id.' and ANGKATAN='.$angkatan.' and KODE_PELATIHAN="'.$kode.'"';
        $query = 'UPDATE peserta_kelas SET DAPAT_KIT='.$status_kit.',DAPAT_SERTIFIKAT='.$status_sertifikat.' '.$kondisi;
        $this->db->query($query);
        
        $query = 'UPDATE peserta SET STATUS_AMBIL_KARTU='.$status_ciccard.' where ID='.$id;
        $this->db->query($query);
        redirect(base_url().'pendaftaran/daftar_con/display_biaya/'.$id.'/'.$kode.'/'.$angkatan);
        
    }
    
    function change_term(){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
		
		$string_term = $this->input->post('pilih_term');
        $res_split = explode(' ', $string_term);
        $no_term = $res_split[1];
        $thn_term = $res_split[2];
        $data_term = $this->db->get_where('term',array('NO_TERM'=>$no_term,'TAHUN'=>$thn_term))->row_array();
        $idterm = $data_term['ID_TERM'];
        $status = $data_term['STATUS'];
        $data_session = array(
            'term'=>$no_term,
            'idterm'=>$idterm,
            'tahun'=>$thn_term,
            'status'=>$status
        );
        
        $this->session->set_userdata($data_session);
        redirect(base_url().'pendaftaran/daftar_con/home');
    }
    
    function menu_report_system(){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
		
		$this->load->view('pendaftaran/report_header');
        $this->load->view('pendaftaran/reporting_system');
    }
    function term_report(){
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
		
		$idterm=$this->session->userdata('idterm');
        $no_term=$this->session->userdata('term');
        $tahun=$this->session->userdata('tahun');
        
        //query untuk mendapatkan total jumlah pendaftar
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm;
        $display['total'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan total pemasukan
        $strquery = "SELECT DISTINCT sum(TOTAL_BAYAR) as jumlah FROM peserta_kelas WHERE ID_TERM=".$idterm;
        $display['total_pemasukan'] = $this->db->query($strquery)->row()->jumlah;
        
        //query untuk mendapatkan jumlah kelas
        $strquery = "SELECT COUNT(KODE_PELATIHAN) as jumlah FROM kelas WHERE TERM=".$no_term." AND TAHUN=".$tahun;
        $display['total_kelas'] = $this->db->query($strquery)->row()->jumlah;
        
		
		for($i=0;$i<7;$i++){
			$display['dist_peserta'][$i]=0;
        }
				
		
		$strquery = 'SELECT GOL
                    FROM user
                    INNER JOIN peserta_kelas ON user.ID = peserta_kelas.ID_USER
                    WHERE ID_TERM ='.$idterm;
        $data = $this->db->query($strquery)->result_array();
		foreach ($data as $value) {
			foreach ($value as $key => $item) {
				//echo $display['dist_peserta'][$item];
				$display['dist_peserta'][$item]++;
			}
		}
		
        //query untuk mendapatkan pelatihan pada term yang aktif
        $strquery = 'select * from pelatihan natural join kelas where TERM='.$this->session->userdata('term');
        $display['hasil'] = $this->db->query($strquery)->result_array();
        
		
		
        $this->load->view('pendaftaran/report_header');
        $this->load->view('pendaftaran/term_report',$display);
    }

	
	function day_report($tgl){
		if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }
		
		$find_date = $tgl;
		
		$this->load->library('date_func');
		
		$idterm=$this->session->userdata('idterm');
        $no_term=$this->session->userdata('term');
        $tahun=$this->session->userdata('tahun');
		
		$objdate = date_create_from_format('Y-m-d', $tgl);
		$tgl = date_format($objdate, 'd.m.Y');
		$hari = date_format($objdate, 'l');
		
		$display['tgl']=$tgl;
		$display['hari']=$this->date_func->day_localize($hari);
		
		$batas_bawah = $find_date.' 00:00:00';
		$batas_atas = $find_date.' 23:59:59';
		
		$qry_pembayaran = $this->db->query('select * from pembayaran where TGL_BAYAR>"'.$batas_bawah.'" and TGL_BAYAR<"'.$batas_atas.'" order by TGL_BAYAR ASC')->result_array();
		
		if(count($qry_pembayaran)==0){
			$display['kosong']=TRUE;
		}else{
			$display['kosong']=FALSE;
			$display['jum_trans']=count($qry_pembayaran);
			$display['masuk']=0;
			$display['keluar']=0;
			for($i=0;$i<$display['jum_trans'];$i++){
				if($qry_pembayaran[$i]['JUMLAH']<0){
					$display['keluar']+=abs($qry_pembayaran[$i]['JUMLAH']);
				}else{
					$display['masuk']+=abs($qry_pembayaran[$i]['JUMLAH']);
				}
			}
			$total_user=$this->db->query('select count(distinct ID_FO) as user from pembayaran where TGL_BAYAR>"'.$batas_bawah.'" and TGL_BAYAR<"'.$batas_atas.'"')->row_array();
			$display['user']=$total_user['user'];
			
			for($i=0;$i<count($qry_pembayaran);$i++){
				$display['detail'][$i]['time']=$this->date_func->extract_time($qry_pembayaran[$i]['TGL_BAYAR']);
				$display['detail'][$i]['jumlah']=$qry_pembayaran[$i]['JUMLAH'];
				if($qry_pembayaran[$i]['JUMLAH']>=0){
					$display['detail'][$i]['keterangan']='Masuk';
				}else{
					$display['detail'][$i]['keterangan']='Keluar';
				}
				$display['detail'][$i]['pelatihan']=$qry_pembayaran[$i]['KODE_PELATIHAN'].'-'.$qry_pembayaran[$i]['ANGKATAN'];
				$qry_nama = $this->db->get_where('user',array('ID'=>$qry_pembayaran[$i]['ID']))->row_array();
				$display['detail'][$i]['atasnama']=$qry_nama['NAMA'];
				$qry_fo = $this->db->get_where('user',array('ID'=>$qry_pembayaran[$i]['ID_FO']))->row_array();
				$display['detail'][$i]['fo']=$qry_fo['NAMA'];
				
			}
		}
		$this->load->view('pendaftaran/report_header');
        $this->load->view('pendaftaran/daily_report',$display);
		
		
		
	}
	
}

?>
